package com.est.dase;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;
/**
*
* @author Fernando Geraldes, Jaime Santos, Jorge Silva
*
*/
public class MapsClass extends MapActivity {

	MapView mapView; 
    MapController mc;
    GeoPoint p;
 
    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
 
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);
 
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.androidmarker);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            
             
//                Toast.makeText(getBaseContext(), 
//                        p.getLatitudeE6() / 1E6 + "," + 
//                        p.getLongitudeE6() /1E6 , 
//                        Toast.LENGTH_SHORT).show();
            
                                       
            return false;
        }         

    } 
	    
	@SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);
        
        Bundle bundle = getIntent().getExtras();
        String extraLat = bundle.getString("lat");
        String extraLon = bundle.getString("lon");
        
        //Initialize map
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.displayZoomControls(true);
        mapView.setSatellite(true);
        mapView.setStreetView(true);
        
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 
 
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

        //Initialize controler
        mc = mapView.getController();
     
        //Set initial position
        //String coordinates[] = {"1.352566007", "103.78921587"};
        //String coordinates[] = {"39.846059", "-7.494368"};
        double lat = Double.parseDouble(extraLat);
        double lng = Double.parseDouble(extraLon);
        p = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));
        mc.animateTo(p);
        mc.setZoom(17); 
 
        //---Add a location marker--- 
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        
 
        mapView.invalidate();
        
    }

	
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent actionDown) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			//startActivity(new Intent(MapsClass.this, TabBar.class));
			MapsClass.this.finish();
			return true;
		}
		return false;
	}
	
}
